<%@ page import="cn.labbook.bluetooth.constant.AdminConstant" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  %>
  <base href="<%=basePath%>">
  <title>经销商页面</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="stylesheet" type="text/css" href="assets/css/admin/reset.css"/>
  <link rel="stylesheet" type="text/css" href="assets/plugins/layui/css/layui.css">
  <link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_893379_qj8lfap4ylj.css"/>
</head>
<style>
  /*让徽章上下居中*/
  .layui-badge {
    top: 50%;
    transform: translateY(-50%);
  }
</style>
<body>
<div>
  <div class=" clearfix margin-15">
    <div class="layui-col-md12">
      <div class="layui-card-header">
        经销商列表
      </div>
      <div class="layui-form layuiadmin-card-header-auto padding-tb-10 layui-card"
           lay-filter="layadmin-userfront-formlist" id="searchForm">
        <div class="layui-form-item searchDiv" style="margin-bottom: 0">
          <div class="layui-inline">
            <label class="layui-form-label" style="text-align: left">用户名称</label>
            <div class="layui-input-block">
              <input type="text" placeholder="请输入用户名称" autocomplete="off"
                     class="layui-input" id="adminName">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">用户身份</label>
            <div class="layui-input-block ">
              <select name="userStatus" id="adminStatus">
                <option value="">请选择</option>
                <option value=<%=AdminConstant.position.DEALER%>>经销商</option>
                <option value=<%=AdminConstant.position.LANDLORD%>>房东</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" id="search"
                    lay-filter="LAY-user-front-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      <div class="layui-tab-content">
        <div class="layui-btn-div">
          <button class="layui-btn" id="server">设置费率</button>
        </div>
        <table class="layui-table" id="payPmList" lay-filter="payPmList"></table>
      </div>
    </div>
  </div>
</div>
<div id="addPmDiv" class="divHideLayer">
  <form class="layui-form" lay-filter="editForm" id="editForm">
    <table class="table_all">
      <tbody>
      <tr>
        <th style="width: 25%"><span class="red">*</span>用户姓名</th>
        <td style="width: 75%">
          <input type="text" name="adminName" lay-verify="required" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <tr id="accountNumber">
        <th><span class="red">*</span>账号</th>
        <td>
          <input type="text" name="accountNumber" lay-verify="required" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <tr id="password">
        <th><span class="red">*</span>密码</th>
        <td>
          <input type="text" name="password" lay-verify="required" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <%--<tr>--%>
      <%--<th><span class="red">*</span>身份</th>--%>
      <%--<td style="width: 35%">--%>
      <%--<select name="adminStatus" id="admin_status" lay-verify="required" lay-search>--%>
      <%--<option value="">请选择</option>--%>
      <%--<option value=<%=AdminConstant.position.MANUFACTURERS%>>厂商</option>--%>
      <%--<option value=<%=AdminConstant.position.DEALER%>>经销商</option>--%>
      <%--</select>--%>
      <%--</td>--%>
      <%--</tr>--%>

      <tr>
        <th style="width: 25%"><span class="red">*</span>性别</th>
        <td style="width: 75%">
          <select name="sex" id="sex" lay-verify="required">
            <option value="">请选择</option>
            <option value=<%=AdminConstant.SEX.W%>>女</option>
            <option value=<%=AdminConstant.SEX.M%>>男</option>
          </select>
        </td>
      </tr>
      <tr>
        <th style="width: 25%">固定电话</th>
        <td style="width: 75%">
          <input type="text" name="telephone" lay-verify="telephone" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <tr>
        <th style="width: 25%">移动电话</th>
        <td style="width: 75%">
          <input type="text" name="mobilephone" lay-verify="mobilephone" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <tr>
        <th style="width: 25%">邮箱</th>
        <td style="width: 75%">
          <input type="text" name="email" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="">
        </td>
      </tr>
      <%--        --%>
      <%--<tr>--%>
      <%--<th style="width: 25%"><span class="red">*</span>服务费</th>--%>
      <%--<td style="width: 75%">--%>
      <%--<select name="sex" id="pay_price" lay-verify="required" lay-search>--%>
      <%--<option value="">请选择</option>--%>
      <%--<option value="100">100</option>--%>
      <%--<option value="200">200</option>--%>
      <%--<option value="300">200</option>--%>
      <%--</select>--%>
      <%--</td>--%>
      <%--</tr>--%>
      </tbody>
    </table>
    <div class="layui-form-item">
      <div class="submitForm">
        <button class="layui-btn" type="button" lay-submit="" lay-filter="editSubmit" id="cancell">提交</button>
        <button type="button" class="layui-btn layui-btn-primary" id="cancel" onclick="hideFun()">取消</button>
      </div>
    </div>
  </form>
</div>
<div id="serverPmDiv" class="divHideLayer">
  <form class="layui-form" lay-filter="serverForm" id="serverForm">
    <table class="table_all">
      <tbody>
      <%--<tr>--%>
      <%--<th><span class="red">*</span>缴费类型</th>--%>
      <%--<td>--%>
      <%--<select name="payType" id="adminStatus">--%>
      <%--<option value="">请选择</option>--%>
      <%--<option value=<%=AmmeterConstant.payType.QTR%>>季度缴费</option>--%>
      <%--<option value=<%=AmmeterConstant.payType.HFYEAR%>>半年缴费</option>--%>
      <%--<option value=<%=AmmeterConstant.payType.YEAR%>>年缴费</option>--%>
      <%--</select>--%>
      <%--</td>--%>
      <%--</tr>--%>
      <tr>
        <th><span class="red">*</span>服务费率</th>
        <td>
          <input type="text" name="payRate" lay-verify="payRate" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="" id="payRate">
        </td>
      </tr>
      </tbody>
    </table>
    <div class="layui-form-item">
      <div class="submitForm">
        <button class="layui-btn" type="button" lay-submit="" lay-filter="serverSubmit" id="cancel4">提交</button>
        <button type="button" class="layui-btn layui-btn-primary" id="cancel5" onclick="hideFun()">取消</button>
      </div>
    </div>
  </form>
</div>
<div id="serverDefaultPmDiv" class="divHideLayer">
  <form class="layui-form" lay-filter="serverDefaultForm" id="serverDefaultForm">
    <table class="table_all">
      <tbody>
      <tr>
        <th><span class="red">*</span>默认电表费率</th>
        <td>
          <input type="text" name="serverPrive" lay-verify="required" autocomplete="off"
                 class="layui-input layui-form-danger" placeholder="" id="payPrice">
        </td>
      </tr>
      </tbody>
    </table>
    <div class="layui-form-item">
      <div class="submitForm">
        <button class="layui-btn" type="button" lay-submit="" lay-filter="serverDefaultSubmit" id="cancel6">提交
        </button>
        <button type="button" class="layui-btn layui-btn-primary" id="cancel7" onclick="hideFun()">取消</button>
      </div>
    </div>
  </form>
</div>
<script type="text/html" id="barDemo">
  <a class="btn-white btn-warning layui-btn-xs" lay-event="edit">编 辑</a>
  <a class="btn-white btn-danger layui-btn-xs" lay-event="delete">删 除</a>
  {{#  if(d.adminStatus == <%=AdminConstant.position.DEALER%> ){ }}
  <a class="btn-white btn-success layui-btn-xs" lay-event="open">设置费率</a>
  {{#  } }}
  {{#  if(d.adminStatus == <%=AdminConstant.position.DEALER%> ){ }}
  <a class="btn-white btn-danger layui-btn-xs" lay-event="resetPwd">重置密码</a>
  {{#  } }}
</script>
</body>
<script src="assets/plugins/Jquery/jquery.min.js"></script>
<script src="assets/plugins/layui/layui.all.js"></script>
<script src="assets/js/common.js"></script>
<script>
  var adminId = null;
  $(function () {
    layui.use(['table', 'form', 'laydate'], function () {
      var table = layui.table,
        form = layui.form;
      var data = [
        {type: 'numbers', title: '序号'},
        {field: 'adminName', title: '姓名', sort: false, align: 'center'},
        {field: 'manufacturersName', title: '所属厂家', sort: false, align: 'center'},
        {
          field: 'sex', title: '性别', sort: false, align: 'center', templet: function (d) {
            if (d.sex == 1) return '<span class="layui-badge layui-bg-blue">男</span>'
            else if (d.sex == 0) return '<span class="layui-badge layui-bg-red">女</span>'
            else return '<span class="layui-badge layui-bg-cyan">未知</span>'
          }
        },
        {field: 'telephone', title: '固定电话', sort: false, align: 'center'},
        {field: 'mobilephone', title: '移动电话', sort: false, align: 'center'},
        {field: 'email', title: '邮箱', sort: false, align: 'center'},
        {
          field: 'adminStatus', title: '用户状态', sort: false, align: 'center', templet: function (d) {
            if (d.adminStatus == <%=AdminConstant.position.DEALER%>) return '<span class="layui-badge">经销商</span>'
            else if (d.adminStatus == <%=AdminConstant.position.MANUFACTURERS%>) return '<span class="layui-badge layui-bg-green">厂商</span>'
            else if (d.adminStatus == <%=AdminConstant.position.LANDLORD%>) return '<span class="layui-badge layui-bg-blue">房东</span>'
            else return '<span class="layui-badge layui-bg-cyan">未知</span>'
          }
        },
        {field: 'payRate', title: '费率', sort: false, align: 'center'},
        {title: '操作', width: 250, sort: false, align: 'center', toolbar: '#barDemo'}
      ];
      table.render(
        // renderTable("#payPmList", 'admin/dealer/info', data)
        renderTable("#payPmList", 'admin/adminPage', data)
      );

      table.on('tool(payPmList)', function (obj) {
        var data = obj.data;
        adminId = data.adminId;
        if (obj.event === "edit") {
          $("#accountNumber").hide();
          $("#password").hide();
          layer.open({
            type: 1,
            title: "编辑",
            btnAlign: "c",
            offset: ['150px'],
            area: ['500px', '450px'],
            content: $("#addPmDiv"),
            success: function () {
              form.val('editForm', {
                "adminName": data.adminName,
                "adminStatus": data.adminStatus,
                "accountNumber": data.accountNumber,
                "password": data.password,
                "sex": data.sex,
                "telephone": data.telephone,
                "mobilephone": data.mobilephone,
                "email": data.email
              })
            },
            end: function (i) {
              layer.close(i);
              hideWithReset("#addPmDiv", "#editForm")
            }
          });
        }
        if (obj.event === "resetPwd") {
          layer.confirm('确定重置该用户密码？', function (index) {
            postAjaxReq("admin/reset/password", {"adminId": data.adminId}, function (res) {
              if (res.code == 1) {
                obj.del();
                layer.msg("操作成功")
                table.reload('payPmList')
                adminId = null
              } else {
                layer.msg(res.msg)
              }

            });
            layer.close(index);
          })
        }
        if (obj.event === "delete") {
          layer.confirm('确定删除该用户？', function (index) {
            postAjaxReq("admin/delete", {"adminId": data.adminId}, function (res) {
              if (res.code == 1) {
                obj.del();
                layer.msg("操作成功")
                table.reload('payPmList')
                adminId = null
              } else {
                layer.msg(res.msg)
              }

            });
            layer.close(index);
          })
        }
        if (obj.event === "open") {
          var data = obj.data;
          layer.open({
            type: 1,
            title: "设置费率",
            btnAlign: "c",
            offset: ['150px'],
            area: ['400px', '250px'],
            content: $("#serverPmDiv"),
            success: function () {
              form.val('serverForm', {
                "payRate": data.payRate
              })
            },
            end: function (i) {
              layer.close(i);
              hideWithReset("#serverPmDiv", "#serverForm")
            }
          });
          // layer.confirm('确定开通该经销商服务费设置权限？', function (index) {
          //     postAjaxReq("admin/edit", {"adminId": data.adminId,"permission":1}, function (res) {
          //         if (res.code == 1) {
          //             obj.del();
          //             layer.msg("操作成功")
          //             table.reload('payPmList')
          //             adminId = null
          //         } else {
          //             layer.msg(res.msg)
          //         }
          //
          //     });
          //     layer.close(index);
          // })
        }
        if (obj.event === "close") {
          layer.confirm('确定关闭该经销商服务费设置权限？', function (index) {
            postAjaxReq("admin/edit", {"adminId": data.adminId, "permission": 0}, function (res) {
              if (res.code == 1) {
                obj.del();
                layer.msg("操作成功")
                table.reload('payPmList')
                adminId = null
              } else {
                layer.msg(res.msg)
              }

            });
            layer.close(index);
          })
        }
      });
      form.verify({
        mobilephone: [/^[1][3,4,5,7,8][0-9]{9}$/, '请正确输入移动电话'],
        telephone: function (value) {
          var check = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
          if ((value != null && value != '' && value.trim() != '') && !check.test(value)) {
            return '请正确输入固定电话'
          }
        }
      });
      $("#addPm").click(function () {
        $("#accountNumber").show();
        $("#password").show();
        layer.open({
          type: 1,
          title: "",
          btnAlign: "c",
          offset: ['50px'],
          area: ['500px', '700px'],
          content: $("#addPmDiv"),
          end: function (i) {
            layer.close(i)
            hideWithReset("#addPmDiv", "#editForm")
          }
        });
      });
      $("#server").click(function () {
        var url = "constant/get/payPrice"
        arrayAjaxReq(url, data, function (res) {
          if (res.code == 1) {
            console.log(res.data)
            $("#payPrice").val(res.data)
            serverData = table.checkStatus('payPmList').data;
            layer.open({
              type: 1,
              title: "默认费率设置",
              btnAlign: "c",
              offset: ['150px'],
              area: ['400px', '200px'],
              content: $("#serverDefaultPmDiv"),
              success: function () {

              },
              end: function (i) {
                layer.close(i)
                hideWithReset("#serverDefaultPmDiv", "#serverDefaultForm")
              }
            });
          } else {
            layer.msg(res.msg)
          }
        })

      });
      form.on('submit(editSubmit)', function (obj) {
        var url = null;
        var data = obj.field
        if (!isEmpty(adminId)) {
          data.adminId = adminId
          url = "admin/edit"
        } else
          url = "admin/add"
        postAjaxReq(url, data, function (res) {
          if (res.code == 1) {
            layer.closeAll();
            table.reload('payPmList');
            layer.msg("保存成功")
            adminId = null
          } else {
            layer.msg(res.msg)
          }
        })
      });
      form.on('submit(serverSubmit)', function (obj) {
        var url = "ammeter/payRate"
        var data = obj.field;
        data.adminId = adminId
        postAjaxReq(url, data, function (res) {
          if (res.code == 1) {
            layer.closeAll();
            table.reload('payPmList');
            layer.msg("保存成功")
            adminId = null
          } else {
            layer.msg(res.msg)
          }
        })
      });
      form.on('submit(serverDefaultSubmit)', function (obj) {
        var url = "constant/setServerPrive"
        var data = obj.field;
        arrayAjaxReq(url, data, function (res) {
          if (res.code == 1) {
            layer.closeAll();
            table.reload('payPmList');
            layer.msg("设置成功")
          } else {
            layer.msg(res.msg)
          }
        })
      });

      $("#search").click(function () {
        var adminName = $("#adminName").val(),
          adminStatus = $("#adminStatus").val();

        table.reload('payPmList', {
          page: {curr: 1},
          where: {
            adminName: adminName,
            adminStatus: adminStatus
          }
        })
      });
    });
  })

  $("#cancel").on("click", function () {
    layer.closeAll()
  });

  function hideFun() {
    layer.closeAll();
    adminId = null
  }

  function hideWithReset(hide, reset) {
    $(hide).hide()
    $(reset)[0].reset()
  }
</script>
</body>
</html>
